package br.fafica.poo2.rightcode.disciplina;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Stack;

import br.fafica.poo2.rightcode.Aplicacao;
import br.fafica.poo2.rightcode.exception.DisciplinaNaoEncontradaException;
import br.fafica.poo2.rightcode.exception.ListaDisciplinaVaziaException;

public class RepositorioDisciplinaPilha implements IRepositorioDisciplina{

	private Stack<Disciplina> disciplinas;
	private static int increment;
	
	public RepositorioDisciplinaPilha(){
		this.disciplinas = new Stack<Disciplina>();
		increment = (getCodigoIncrement()+1);
		System.err.println("CONTRUTOR DE PILHA DISCIPLINA");
	}
	
	private int getCodigoIncrement(){
		return disciplinas.size();
	}

	@Override
	public void TesteDisciplina() throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void InstalarDisciplina(boolean samples) throws SQLException {
		if(samples){
			Aplicacao app = new Aplicacao();
			for(Disciplina d: app.getDisciplina()){
				AdicionarDisciplina(d);
			}
		}
		
	}

	@Override
	public void AdicionarDisciplina(Disciplina disciplina) throws SQLException {
		if(disciplina.getCodigo()==0){
			disciplina.setCodigo(increment);
			increment+=1;
		}
		disciplinas.push(disciplina);
	}

	@Override
	public void RemoverDisciplina(Disciplina disciplina) throws SQLException, EmptyStackException,
		DisciplinaNaoEncontradaException {
		disciplinas.pop();
	}

	@Override
	public Disciplina BuscarDisciplina(int codigo) throws SQLException, DisciplinaNaoEncontradaException {
		Disciplina busca = null;
		for(Disciplina disciplina: disciplinas){
			if(codigo == disciplina.getCodigo()){
				busca = disciplina;
			}
		}
		return busca;
	}

	@Override
	public ArrayList<Disciplina> ListaDisciplina()
			throws ListaDisciplinaVaziaException, SQLException {
		ArrayList <Disciplina> array = new ArrayList();
		array.addAll(disciplinas);
		return array;
	}

	@Override
	public void EditarDisciplinas(Disciplina disciplina)
			throws DisciplinaNaoEncontradaException, SQLException {
		// TODO Auto-generated method stub
		
	}
		
}
